<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/vue-2.6.12.js"></script>
    <script src="./js/vue-router-3.4.3.js"></script>
</head>

<body>
    <div id="app">
        <!-- 使用路由 -->
        <div>
            <router-link to="/user/id/1/username/zhangsan">去a路由</router-link>
        </div>
        <div>
            <router-view></router-view>
        </div>
    </div>
    <script>
        // 声明组件
        let myA = {
            data() {
                return {
                    id: null,
                    username: '',
                }
            },
            // created() {
            //     // 接收路由器对象传递的参数
            //     // 路由对象接收
            //     console.log(this.$route, "路由对象");
            //     this.id = this.$route.params.id;
            //     this.username = this.$route.params.username;
            // },
            // watch:{
            //     $route(to,from){
            //         // to是新路由，from是旧路由
            //         console.log(to,from,'监听路由参数变化');
            //         this.id = this.$route.params.id;
            //         this.username = this.$route.params.username;
            //     },
            // },
            beforeRouteUpdate(to,from,next){
                console.log(to.params);
                this.id=to.params.id;
                this.username=to.params.username;
                // 继续向下执行
                // next()
                // 阻止继续向下
                next(false)
            },
            template: `
                <div>A组件-{{id}}-{{username}}</div>
            `
        };
        // 创建一个路由对象数组
        let routes = [
            {
                path: '/user/id/:id/username/:username',
                component: myA
            }
        ];
        // 创建一个路由器对象实例
        let router = new VueRouter({
            routes: routes
        })
        new Vue({
            // 注册路由
            router,
            components: {
                'my-a': myA
            },
            el: "#app",
            data: {

            },
            methods: {

            },
        })
    </script>
</body>

</html>